package com.dinzeer.sjapadder.sa;

import com.dinzeer.legendreliclib.lib.util.WaitingTick;
import com.dinzeer.legendreliclib.lib.util.slashblade.SlashBladeUtil;
import mods.flammpfeil.slashblade.SlashBlade;
import mods.flammpfeil.slashblade.entity.EntityJudgementCut;
import mods.flammpfeil.slashblade.item.ItemSlashBlade;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.phys.AABB;
import net.minecraft.util.Mth;
import java.util.List;
import java.util.Random;

import static com.dinzeer.sjapadder.Config.countSuperBloodCut;

public class SuperBloodCut {

    public static void summonJudgementCut(LivingEntity playerIn) {
        // 获取玩家周围5格内的所有实体（排除玩家自身）
        AABB area = new AABB(
                playerIn.getX() - 5.0,
                playerIn.getY() - 2.5,
                playerIn.getZ() - 5.0,
                playerIn.getX() + 5.0,
                playerIn.getY() + 2.5,
                playerIn.getZ() + 5.0
        );

        List<Entity> entities = playerIn.level().getEntities(playerIn, area);
        entities.remove(playerIn); // 确保排除玩家自身

        Random rand = new Random();
        int entityCount = entities.size();
        int totalCount = countSuperBloodCut; // 次元斩默认的生成数量是25个

        // 第一部分：在实体位置生成次元斩
        for (int i = 0; i < Math.min(entityCount, totalCount); i++) {
            int finalI = i;
            WaitingTick.schedule(i, () -> {
                Entity target = entities.get(finalI);

                // 在目标实体位置生成次元斩
                createJudgementCut(
                        playerIn,
                        target.getX(),
                        target.getY() + target.getBbHeight() * 0.5,
                        target.getZ(),
                        rand
                );
            });
        }

        // 第二部分：如果实体数量未达到totalCount，在玩家周围随机散布剩余次元斩
        int remaining = totalCount - Math.min(entityCount, totalCount);
        for (int i = 0; i < remaining; i++) {
            WaitingTick.schedule(i, () -> {
                double angle = rand.nextDouble() * Math.PI * 2;
                double distance = 3.0 + rand.nextDouble() * 5.0; // 1-5格距离

                double x = playerIn.getX() + Mth.cos((float)angle) * distance;
                double y = playerIn.getY() + (rand.nextDouble() - 0.5) * 3.0+1;
                double z = playerIn.getZ() + Mth.sin((float)angle) * distance;

                createJudgementCut(playerIn, x, y, z, rand);
            });
        }
    }

    private static void createJudgementCut(LivingEntity owner, double x, double y, double z, Random rand) {
        if (owner.getMainHandItem().getItem() instanceof ItemSlashBlade){
        // 添加随机偏移使效果更自然
        double offsetX = (rand.nextDouble() - 0.5) +1;
        double offsetY = (rand.nextDouble() - 0.5) +1;
        double offsetZ = (rand.nextDouble() - 0.5) +1;

        EntityJudgementCut judgementCut = new EntityJudgementCut(
                SlashBlade.RegistryEvents.JudgementCut,
                owner.level()
        );

        judgementCut.setPos(
                x + offsetX,
                y + offsetY,
                z + offsetZ
        );

        judgementCut.setIsCritical(true);
        judgementCut.setDamage(SlashBladeUtil.getBaseAttackModifier(owner.getMainHandItem())*0.1);
        judgementCut.setColor(0xFF0000);
        judgementCut.setOwner(owner);
        owner.level().addFreshEntity(judgementCut);
        }
    }
}